package cn.shyouchi.wuguanjia.android.ui.patrol.begin;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jeanboy.cropview.cropper.CropperHandler;
import com.jeanboy.cropview.cropper.CropperManager;
import com.jeanboy.cropview.cropper.CropperParams;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.components.storage.UserStorage;
import cn.shyouchi.wuguanjia.android.event.PatrolInfoChangeEvent;
import cn.shyouchi.wuguanjia.android.impl.ModuleItemClickListener;
import cn.shyouchi.wuguanjia.android.utils.LogUtil;
import cn.shyouchi.wuguanjia.android.widget.GlideCircleTransform;
import cn.shyouchi.wuguanjia.android.widget.TopBar;

public class PatrolRingDetailActivity extends BaseActivity implements ModuleItemClickListener<PatrolRingDesEntity.ResultMessageBean.InfoBean>
        , CropperHandler {

    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.img_header)
    ImageView mImgHeader;
    @BindView(R.id.tv_person_name)
    TextView mTvPersonName;
    @BindView(R.id.tv_patrol_post_name)
    TextView mTvPatrolPostName;
    @BindView(R.id.rv_ring_detail)
    RecyclerView mRvRingDetail;

    PatrolRingDetailAdapter mPatrolRingDetailAdapter;
    List<PatrolRingDesEntity.ResultMessageBean.InfoBean> mPatrolRingDetailEntities = new ArrayList<>();
    PatrolRingDesEntity.ResultMessageBean model;

    private final String BASE_URL = "http://118.178.88.132:8000";

    UserStorage mUserStorage;

    PatrolRingDesEntity.ResultMessageBean.InfoBean currentInfo;


    @Override
    public int initContentView() {
        return R.layout.activity_patrol_ring_detail;
    }

    @Override
    public void initInjector() {
        mUserStorage = new UserStorage(mContext);
        model = getIntent().getParcelableExtra("model");
        mPatrolRingDetailEntities.addAll(model.getInfo());


    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.setTitle(getTitle().toString());
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        setSupportActionBar(mTopBar.getToolbar());

        EventBus.getDefault().register(this);

//        mPatrolRingDetailEntities.add(new PatrolRingDetailEntity());
//        mPatrolRingDetailEntities.add(new PatrolRingDetailEntity());
//        mPatrolRingDetailEntities.add(new PatrolRingDetailEntity());
//        mPatrolRingDetailEntities.add(new PatrolRingDetailEntity());
//        mPatrolRingDetailEntities.add(new PatrolRingDetailEntity());
//        mPatrolRingDetailEntities.add(new PatrolRingDetailEntity());
//        mPatrolRingDetailEntities.add(new PatrolRingDetailEntity());
//        mPatrolRingDetailEntities.add(new PatrolRingDetailEntity());

        mPatrolRingDetailAdapter = new PatrolRingDetailAdapter(mPatrolRingDetailEntities, this);
        mRvRingDetail.setLayoutManager(new LinearLayoutManager(mContext));
        mRvRingDetail.setAdapter(mPatrolRingDetailAdapter);

        bindPersonDetail();
    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }


    private void bindPersonDetail() {
        mTvPersonName.setText(String.format("%1s(%2s)", mUserStorage.getUserData().getName(), mUserStorage.getUserData().getPermissions_name()));
//        mTvPatrolPostName.setText("巡检岗位：1号岗");
        loadHead(BASE_URL + mUserStorage.getUserData().getAvatar());
    }

    private void loadHead(String path) {
        Glide.with(mContext)
                .load(path)
                .placeholder(R.mipmap.ic_default_header)
                .transform(new GlideCircleTransform(mContext))
                .into(mImgHeader);
    }

    @Override
    public void moduleItemClick(PatrolRingDesEntity.ResultMessageBean.InfoBean model) {
        currentInfo = model;

        //注释掉二维码

//        if (model.getInspectionmethod() == 2) {
//            Intent intent = new Intent(mContext, AttendanceTokePhotoActivity.class);
//            intent.putExtra("info", model);
//            intent.putExtra("camera_flag", 0);
            CropperManager.getInstance().pickFromCamera();
//            startActivityForResult(intent, AttendanceTokePhotoActivity.REQUEST_CODE);
//        } else {
//            startActivity(new Intent(mContext, QRCodeActivity.class));
//        }
    }

    @Override
    public void moduleItemLongClick(PatrolRingDesEntity.ResultMessageBean.InfoBean model) {

    }

    @Override
    public void onDel(PatrolRingDesEntity.ResultMessageBean.InfoBean model) {

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        CropperManager.getInstance().handlerResult(requestCode, resultCode, data, false);

//        if (requestCode == AttendanceTokePhotoActivity.REQUEST_CODE && resultCode == AttendanceTokePhotoActivity.RESULT_CODE) {
//            data.setClass(mContext, PatrolSubmitActivity.class);
//            startActivity(data);
//        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onPatrolChange(PatrolInfoChangeEvent event) {
        mPatrolRingDetailEntities.clear();
        mPatrolRingDetailEntities.addAll(event.getModel().getInfo());
        mPatrolRingDetailAdapter.notifyDataSetChanged();
    }


    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    public CropperParams getParams() {
        return null;
    }

    @Override
    public void onCropped(Uri uri) {
        LogUtil.e(uri.toString());
        Intent intent = new Intent(mContext, PatrolSubmitActivity.class);
        intent.putExtra("info", currentInfo);
        intent.putExtra("path", uri.getPath());
        startActivity(intent);

    }

    @Override
    public void onCropCancel() {

    }

    @Override
    public void onCropFailed(String msg) {
        LogUtil.e(msg);
    }


    @Override
    protected void onResume() {
        CropperManager.getInstance().build(this);
        super.onResume();
    }
}
